﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GestorDeFlotasDesktop.AbmRol
{
    public partial class Form1 : MasterPage
    {
        ArrayList array = new ArrayList();
        Form ventanPadre;
        public capaDatos.SQLSyntaxisGeneration rolCD = new capaDatos.SQLSyntaxisGeneration();
        DataTable roles, rolesHab;
        DataTable rolBack;
        DataTable roles2; 
        public Form1( Form ventana)
        {
            InitializeComponent();
            pnRoles.Visible = false;
            ocultarMenu(ref ventanPadre, ventana);
                      
        }

        private void button2_Click(object sender, EventArgs e)
        {
           pnRoles.Visible=true;
           panel1.Visible = true;
           btBaja.Text = "Cancelar";
            if(btAlta.Text=="Cancelar"){

                pnRoles.Visible = false;
                BtbBuscar.Visible = true;
                btAlta.Text = "Alta";
                btBaja.Text = "Baja";
            
            }
            if (array.Count != 0)
            {
                btAlta.Text = "Confirmar";
               
                int num = Convert.ToInt32(array[array.Count-1]);

                Boolean valor = Convert.ToBoolean(rolesHab.Rows[0].ItemArray[0]);
                if (chrolHabilitado.Checked && !valor)
                {
                    //está habilitando de nuevo un rol
                   rolCD.executeSp("PELUSA.deshabilitarHabilitarRol", parametrosStoredProcedure( paramsNames("id", "value"), paramsValues(roles.Rows[num].ItemArray[0], "0")));
                   
                }
                if (!chrolHabilitado.Checked && valor)
                {
                    //ese quiere deshabilitar desde modificación
                    MessageBox.Show("No se permite deshabilitar roles desde la modificación. Para inhabilitar un rol, presione el botón de Baja");

                }
                if(rol.Text =="")
                    rol.Text =Convert.ToString(rolesHab.Rows[0].ItemArray[1]);
                
                
                rolCD.executeSp("PELUSA.modificarF", parametrosStoredProcedure( paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 1,Convert.ToInt32(chCliente.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 2, Convert.ToInt32(chRol.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 3, Convert.ToInt32(chUser.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 4, Convert.ToInt32(chAuto.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 5, Convert.ToInt32(chbReloj.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 6, Convert.ToInt32(chbChofer.Checked))));
                rolCD.executeSp("PELUSA.modificarF", parametrosStoredProcedure( paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 7, Convert.ToInt32(chbTurno.Checked))));
                rolCD.executeSp("PELUSA.modificarF", parametrosStoredProcedure( paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 8, Convert.ToInt32(chbChoferAuto.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 9, Convert.ToInt32(chbRegistroViajes.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 10, Convert.ToInt32(chbRendicionCtaChofer.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 11, Convert.ToInt32(chbFacturaCliente.Checked))));
                rolCD.executeSp("PELUSA.modificarF",parametrosStoredProcedure(  paramsNames("id", "fun", "value"), paramsValues(roles.Rows[num].ItemArray[0], 12, Convert.ToInt32(chbListadoEstaditico.Checked))));

                /* fun = numero de item */
                rolCD.executeSp("PELUSA.cambioNombreRol", parametrosStoredProcedure(paramsNames("id", "nombreRol"), paramsValues(roles.Rows[num].ItemArray[0], rol.Text)));
                
                string query = "select * from [PELUSA].roles ";
               
                roles = rolCD.executeQuery(query);
                this.dataGridView1.DataSource = roles;
                
                btAlta.Text = "Alta";
                panel1.Visible = true;
                pnRoles.Visible= true;
                btBaja.Visible = true;



            }

            else if ((btAlta.Text == "Alta"))
            {
                dataGridView1.Visible = false;
                BtbBuscar.Visible = false;
                btAlta.Text = "Confirmar";
                btBaja.Text = "Cancelar";
                //esto es un alta
            }
            else if (!dataGridView1.Visible && btAlta.Text == "Confirmar")
            {
                BtbBuscar.Visible = false;
                
                

                //esto es unchecked alta a confirmar ya
                if (rol.Text =="")
                {
                    MessageBox.Show("Por favor ingrese el nombre del Rol a crear", "ABM Roles - Alta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
                else
                {
                 DataTable auxRol= rolCD.executeSp("PELUSA.insertarNombreRol", parametrosStoredProcedure( paramsNames("nombreRol"), paramsValues(rol.Text)));

                  
                    int id=Convert.ToInt32(auxRol.Rows[0].ItemArray[0]);//id del nuevo rol
                    
                    if(chCliente.Checked)
                        rolCD.executeSp("PELUSA.agregarF", parametrosStoredProcedure(paramsNames("id", "fun"), paramsValues(id, 1)));
                    if(chRol.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 2)));
                    if(chUser.Checked)
                        rolCD.executeSp("PELUSA.agregarF", parametrosStoredProcedure(paramsNames("id", "fun"), paramsValues(id, 3)));
                    if(chAuto.Checked)
                        rolCD.executeSp("PELUSA.agregarF", parametrosStoredProcedure( paramsNames("id", "fun"), paramsValues(id, 4)));
                    if(chbReloj.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 5)));
                    if(chbChofer.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 6)));
                    if(chbTurno.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 7)));
                    if(chbChoferAuto.Checked)
                        rolCD.executeSp("PELUSA.agregarF", parametrosStoredProcedure( paramsNames("id", "fun"), paramsValues(id, 8)));
                    if(chbRegistroViajes.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 9)));
                    if(chbRendicionCtaChofer.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 10)));
                    if(chbFacturaCliente.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 11)));
                    if (chbListadoEstaditico.Checked)
                        rolCD.executeSp("PELUSA.agregarF",parametrosStoredProcedure(  paramsNames("id", "fun"), paramsValues(id, 12)));


                    dataGridView1.ClearSelection();
                    dataGridView1.Visible = true;
                    btAlta.Text = "Alta";
                    BtbBuscar.Visible = true;
                    panel1.Visible = false;
                    
                    btBaja.Visible = true;
                    string query = "select * from [PELUSA].roles";
                    if (rol.Text != "")
                    {
                        query += " where nombreRol like '%" + rol.Text + "%'";
                    }

                    roles = rolCD.executeQuery(query);
                    chrolHabilitado.Checked = true;
                    this.dataGridView1.DataSource = roles;
                    rol.Visible = true;
                    pnRoles.Visible = true;

                    rolBack = roles;

                }

            }
   
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         pnRoles.Visible = false;
         panel1.Visible = true;
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

           
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int bandera = 0;
            if (btAlta.Text == "Cancelar")//bandera para indicar que no se modifique el texto de los botones si es una baja
                bandera = 1;
            setearCheckBox(e.RowIndex, array, dataGridView1, pnRoles , btAlta);

            if ((array.Contains(e.RowIndex)))
            { 
                int id =Convert.ToInt32(roles.Rows[e.RowIndex].ItemArray[0]);

                string query = "select idAccion from [PELUSA].acciones_roles where idRol = ";
                query += Convert.ToString(id);
                 roles2  = rolCD.executeQuery(query);
                 int funcionalidades = roles2.Rows.Count - 1;
                while (funcionalidades >= 0) {


                    switch (Convert.ToInt32(roles2.Rows[funcionalidades].ItemArray[0]))
                        {
                            case 1: chCliente.Checked = true; break;
                            case 2: chRol.Checked = true; break;
                            case 3: chUser.Checked = true; break;
                            case 4: chAuto.Checked = true; break;
                            case 5: chbReloj.Checked = true; break;
                            case 6: chbChofer.Checked = true; break;
                            case 7: chbTurno.Checked = true; break;
                            case 8: chbChoferAuto.Checked = true; break;
                            case 9: chbRegistroViajes.Checked = true; break;
                            case 10: chbRendicionCtaChofer.Checked = true; break;
                            case 11: chbFacturaCliente.Checked = true; break;
                            case 12: chbListadoEstaditico.Checked = true; break;

                        
                    }
                funcionalidades--;
                

                }

                query.Remove(0);
                query = "select activoRol,nombreRol from [PELUSA].roles where idRol = ";
                query += Convert.ToString(id);
                rolesHab = rolCD.executeQuery(query);
                if (!Convert.ToBoolean(rolesHab.Rows[0].ItemArray[0]))
                    chrolHabilitado.Checked = false;
                else
                    chrolHabilitado.Checked = true;
/*1  ABM de Cliente
2  ABM de Rol
3  ABM de Usuario
4  ABM de Auto
5  ABM de Reloj
6  ABM de Chofer
7  ABM de Turno
8  Asignacion Chofer-auto
9  Registro de viajes
10 Rendicion de cuenta de Chofer
11 Facturacion a cliente
12 Listado estadistico
*/
                btBaja.Text = "Cancelar";

                pnRoles.Visible = true;
                panel1.Visible = true;
                btBaja.Visible = true;
                btAlta.Visible = true;
                if (bandera == 1) {
                    btAlta.Text = "Cancelar";
                    btBaja.Text = "Confirmar";
                }

            }
            else
            {
                pnRoles.Visible = false;
                btAlta.Text = "Alta";
                btBaja.Text = "Baja";
            }


        }

        private void BtbBuscar_Click(object sender, EventArgs e)
        {
            string query = "select * from [PELUSA].roles";
            if (rol.Text != "")
            {
                query += " where nombreRol like '%" + rol.Text + "%'";
            }

            roles =  rolCD.executeQuery(query);
            
            this.dataGridView1.DataSource = roles;
            rol.Visible = true;
            pnRoles.Visible = true;

           rolBack= roles;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void btBaja_Click(object sender, EventArgs e)
        {
            btAlta.Text = "Cancelar";
            BtbBuscar.Visible = true;
            btAlta.Visible = false;
            dataGridView1.Visible = true;
            panel1.Visible = true;
            pnRoles.Visible = true;

            if (btBaja.Text == "Cancelar")
            {

                pnRoles.Visible = false;
                BtbBuscar.Visible = true;
                btAlta.Text = "Alta";
                btBaja.Text = "Baja";

            }
            if (btBaja.Text != "Confirmar"&&btBaja.Text!="Cancelar")
            {
                btBaja.Text = "Confirmar";
                btAlta.Text = "Cancelar";
               
            }

            else if (btBaja.Text == "Confirmar" && btAlta.Text == "Cancelar" && array.Count == 0)
            {
                MessageBox.Show("No se ha seleccionado ningún rol", "ABM Roles - Baja", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


            }
            else if (btBaja.Text == "Confirmar" && array.Count != 0)
            {
                int num = Convert.ToInt32(array[array.Count - 1]);
               Boolean valor= Convert.ToBoolean(rolesHab.Rows[0].ItemArray[0]);
                if ((!chrolHabilitado.Checked)  && (!valor))
                {
                    MessageBox.Show("El rol seleccionado ya se encuentra deshabilitado.", "ABM Rol - Baja", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }

                else
                {
                    if (btBaja.Text != "Cancelar")
                    {
                        int id = Convert.ToInt32(rolBack.Rows[num].ItemArray[0]);
                        rolCD.executeSp("PELUSA.deshabilitarHabilitarRol", parametrosStoredProcedure(paramsNames("id", "value"), paramsValues(id, "0")));



                        string query = "select * from [PELUSA].roles where ";

                        query += "idRol=" + rolBack.Rows[num].ItemArray[0];

                        roles = rolCD.executeQuery(query);
                        this.dataGridView1.DataSource = roles;
                        chrolHabilitado.Checked = false;
                    }
                }
                btBaja.Text = "Baja";
                btAlta.Text = "Alta";
                BtbBuscar.Visible = true;
               
            }
            
            btAlta.Visible = true;
            btBaja.Visible = true;
            dataGridView1.Visible = true;
            rol.Visible = true;

        }



        private void AbmRol_FormClosing(object sender, FormClosingEventArgs e)
        {
             ventanPadre.Visible = true;
        }

        private void Limpiar_Click(object sender, EventArgs e)
        {
           
                rol.Text = "";
                chCliente.Checked = false;
                chRol.Checked = false;
                chUser.Checked = false;
                chAuto.Checked = false;
                chbReloj.Checked = false;
                chbChofer.Checked = false;
                chbTurno.Checked = false;
                chbChoferAuto.Checked = false;
                chbRegistroViajes.Checked = false;
                chbRendicionCtaChofer.Checked = false;
                chbFacturaCliente.Checked = false;
                chbListadoEstaditico.Checked = false;
                chrolHabilitado.Checked = false;
           

                if (btBaja.Text == "Baja"&&btAlta.Text=="Alta")
                {
                    pnRoles.Visible = false;
                    BtbBuscar.Visible = true;
                    dataGridView1.ClearSelection();

                }

        }

     


    }
}
